####################################################################################################################
# Replication file for Treger:                                                                                     #
# Changing the lens: The contingency of results from conjoint experiments on the outcome variable and the estimand #
#                                                                                                                  #
# Reanalysis of: 2.	Lehrer, Stöckle, and Juhl (2022): Assessing the relative influence of party unity on vote
# choice: Evidence from a conjoint experiment. 
# 
# Created by Clareta Treger                                                                                        #
# Last edited on 15 May 2025                                                                                       #
####################################################################################################################

#----------
#Cleaning House
rm(list=ls()) 
if(is.null(dev.list())==FALSE)dev.off() 
cat("\014") 
#----------


#loading required packages
install.packages("tidyverse")
install.packages(c("nycflights13", "gapminder", "Lahman"))
install.packages("cregg")

library(data.table)
library(survival)
library(MASS)
library(tidyverse)
library(knitr)
library(kableExtra)
library(cjoint)
library("cregg")
library(tinytex)

#Uploading Leher's data
load("Leher_replication_data.Rdata")


#Using cregg to estimate the model using Leher's data 
t1 <- chosen~     dist +
                  critique +
                  parliament +
                  conference +
                  reform +
                  role +
                  gender +
                  age +
                  job 

#########################################################
# Table D3: AMCEs and Marginal Means                    #
#########################################################
#AMCEs table (Table D3)

amces <- cj(dat, chosen ~ dist +
              critique +
              parliament +
              conference +
              reform +
              role +
              gender +
              age +
              job, id = ~id_g)  


head(amces[c("feature", "level", "estimate", "std.error")], 30L)
write.csv(amces, "Leher_cregg_amces.csv", row.names=FALSE)


#MMs table (Table D3)
t2 <- cj(dat, t1, id = ~id_g, estimate = "mm", h0 = 0.5) #produce a table with mms
write.csv(t2, "Leher_cregg_mms.csv", row.names=FALSE)

#########################################################
# Figure D3: AMCEs and Marginal Means                   #
#########################################################

#AMCEs plot (Figure D3, upper panel)
plot(amces, vline = 0.0, xlim = c(-0.5,0.1))
ggsave("AMCE_Leher1.pdf", units="cm", width=17.7, height=17.7)

#MMs plot (Figure D3, bottom panel)

plot(mm(dat, t1, id = ~id_g), vline = 0.5, xlim = c(0.15,0.7))
ggsave("MM_Leher2.pdf", units="cm", width=17.7, height=17.7)



